package com.miguo.activity;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.ImageView;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.CustomAutoCompleteTextView;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;

import java.util.HashMap;
import java.util.Map;

public class ResetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private BaseHeadView mBaseHead;

    private CustomAutoCompleteTextView mOriginalPwdEdt, mPwdEdt, mConfirmPwdEdt;

    private ImageView mHidePwdIv;

    private boolean isHidePwd = false;

    private String mOriginalPassword, mPassword, mConfirmPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        initView();
        initListener();
    }

    private void initView() {
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setTitle("重设密码");

        mOriginalPwdEdt = (CustomAutoCompleteTextView) findViewById(R.id.reset_original_pwd_edt);
        mPwdEdt = (CustomAutoCompleteTextView) findViewById(R.id.reset_pwd_edt);
        mConfirmPwdEdt = (CustomAutoCompleteTextView) findViewById(R.id.reset_confirm_pwd_edt);
        mHidePwdIv = (ImageView) findViewById(R.id.reset_hide_confirm_pwd_iv);
    }

    private void initListener() {
        mHidePwdIv.setOnClickListener(this);
        findViewById(R.id.reset_btn).setOnClickListener(this);
    }

    private boolean verify() {
        mOriginalPassword = mOriginalPwdEdt.getText().toString();
        mPassword = mPwdEdt.getText().toString();
        mConfirmPassword = mConfirmPwdEdt.getText().toString();

        if (mOriginalPassword.length() == 0) {
            mOriginalPwdEdt.setShakeAnimation();
            Toast.show(mContext, "请输入旧密码");
            return false;
        }

        if (mPassword.length() == 0) {
            mPwdEdt.setShakeAnimation();
            Toast.show(mContext, "请输入新密码");
            return false;
        }

        if (mConfirmPassword.length() == 0) {
            mConfirmPwdEdt.setShakeAnimation();
            Toast.show(mContext, "请输入确认新密码");
            return false;
        }

        if (mOriginalPassword.length() < 6 || mOriginalPassword.length() > 16) {
            mOriginalPwdEdt.setShakeAnimation();
            Toast.show(mContext, "旧密码格式错误");
            return false;
        }

        if (mPassword.length() < 6 || mPassword.length() > 16) {
            mPwdEdt.setShakeAnimation();
            Toast.show(mContext, "新密码格式错误");
            return false;
        }

        if (mConfirmPassword.length() < 6 || mConfirmPassword.length() > 16) {
            mConfirmPwdEdt.setShakeAnimation();
            Toast.show(mContext, "确认新密码格式错误");
            return false;
        }

        if (mConfirmPassword.equals(mPassword) == false) {
            mConfirmPwdEdt.setShakeAnimation();
            Toast.show(mContext, "两次密码输入不一致");
            return false;
        }
        return true;
    }

    private void resetPassword() {
        AjaxCallBack callBack = new AjaxCallBack(mActivity, "正在重设密码,请稍候...") {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);
                Toast.show(mContext, json.optString("info"));
                if (json.optInt("status") == 200) {
                    AppContext.UserInfo().Logout();
                    setResult(RESULT_OK);
                    AppHelper.StartLogin(mActivity);
                    UserAccountActivity.instance.finish();
                    ResetPasswordActivity.this.finish();
                    return;
                }
            }
        };
        Map<String, String> param = new HashMap<String, String>();
        param.put("original_password", mOriginalPassword);
        param.put("password", mPassword);
        param.put("confirm_password", mConfirmPassword);
        param.put("token", AppContext.UserInfo().getToken());
        HttpClient.postEncryptData(Url.Passport.Reset.Password, param, callBack);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reset_hide_confirm_pwd_iv:
                if (isHidePwd) {
                    //隐藏密码
                    isHidePwd = false;
                    mConfirmPwdEdt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    mConfirmPwdEdt.setSelection(mConfirmPwdEdt.getText().toString().length());
                    mHidePwdIv.setImageResource(R.drawable.ico_login_eye_h);
                } else {
                    //显示密码
                    isHidePwd = true;
                    mConfirmPwdEdt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    mConfirmPwdEdt.setSelection(mConfirmPwdEdt.getText().toString().length());
                    mHidePwdIv.setImageResource(R.drawable.ico_login_eye);
                }
                break;
            case R.id.reset_btn:
                if (verify()) {
                    resetPassword();
                }
                break;
        }
    }
}
